{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第三章实例"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 书店销售策略"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "您购买了价格100元的图书100册，付款7500.0元\n"
     ]
    }
   ],
   "source": [
    "price=eval(input(\"请输入图书价格：\"))\n",
    "books=int(input(\"请输入购买册数：\"))\n",
    "flag=int(input(\"有会员卡，输入1，否则输入0：\"))\n",
    "if flag==1:\n",
    "    if books>=5:\n",
    "        payment=price*0.75*books\n",
    "    else:\n",
    "        payment=price*0.85*books\n",
    "else:\n",
    "    if books>=5:\n",
    "        payment=price*0.85*books\n",
    "    else:\n",
    "        payment=price*0.95*books\n",
    "print(\"您购买了价格{}元的图书{}册，付款{}元\".format(price,books,payment))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 99乘法表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "2*1=2\t\n",
      "3*1=3\t3*2=6\t\n",
      "4*1=4\t4*2=8\t4*3=12\t\n",
      "5*1=5\t5*2=10\t5*3=15\t5*4=20\t\n",
      "6*1=6\t6*2=12\t6*3=18\t6*4=24\t6*5=30\t\n",
      "7*1=7\t7*2=14\t7*3=21\t7*4=28\t7*5=35\t7*6=42\t\n",
      "8*1=8\t8*2=16\t8*3=24\t8*4=32\t8*5=40\t8*6=48\t8*7=56\t\n",
      "9*1=9\t9*2=18\t9*3=27\t9*4=36\t9*5=45\t9*6=54\t9*7=63\t9*8=72\t\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):\n",
    "    for j in range(1,i):\n",
    "        print(f\"{i}*{j}={i*j}\",end=\"\\t\")\n",
    "    print(\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. 计算圆周率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PI=3.141590653589692\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "s=1.0  \n",
    "n=1.0  \n",
    "t=1.0  \n",
    "pi=0.0  \n",
    "while math.fabs(t)>=1e-6:\n",
    "    pi+=t\n",
    "    n+=2\n",
    "    s=-s\n",
    "    t=s/n\n",
    "pi=pi*4\n",
    "print(\"PI={}\".format(pi))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. 对手名单"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "对战顺序是：\n",
      "a--y\n",
      "b--x\n",
      "c--z\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for i in \"xyz\":   #甲方a选择乙方对手\n",
    "    for j in \"xyz\":  #甲方b选择乙方对手\n",
    "        if i!=j:\n",
    "            for k in \"xyz\":  #甲方c选择乙方对手\n",
    "                if (i!=k) and (j!=k):\n",
    "                    if (i!=\"x\") and (k!=\"x\") and (k!=\"y\"):\n",
    "                        print(\"对战顺序是：\\na--{}\\nb--{}\\nc--{}\\n\".format(i,j,k))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. 猜数字游戏"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "猜大了\n",
      "猜大了\n",
      "猜大了\n",
      "猜小了\n",
      "猜小了\n",
      "猜小了\n",
      "猜大了\n",
      "猜大了\n",
      "猜小了\n",
      "猜小了\n",
      "猜小了\n",
      "猜小了\n",
      "猜小了\n",
      "猜中了\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "target=random.randint(1,1000)\n",
    "while True:\n",
    "    guess=eval(input(\"输入一个整数:\"))\n",
    "    print(guess)\n",
    "    if guess<target:\n",
    "        print(\"猜小了\")\n",
    "    elif guess>target:\n",
    "        print(\"猜大了\")\n",
    "    else:\n",
    "        print(\"猜中了\")\n",
    "        break\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
